import 'package:flutter/material.dart';

enum LoadState { LOADING, LOAD_FAIL }

class LoadStatePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('加载状态页面'),
      ),
      body: _buildBody(context),
    );
  }

  Widget _buildBody(BuildContext context) {
    final Object? args = ModalRoute.of(context)?.settings.arguments;
    if (args is LoadState) {
      final LoadState loadState = args;
      return loadState == LoadState.LOAD_FAIL ? _buildLoadFail() : _buildLoading();
    }
    return _buildLoading();
  }

  Widget _buildLoading() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const <Widget>[
          SizedBox(
            height: 30,
            width: 30,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation<Color>(Colors.lightBlue),
            ),
          ),
          SizedBox(height: 20),
          Text(
            '加载中，请稍后',
            style: TextStyle(
              fontSize: 16,
            ),
          )
        ],
      ),
    );
  }

  Widget _buildLoadFail() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            'assets/image/404.jpeg',
            width: 200,
            height: 200,
          ),
          const SizedBox(height: 20),
          const Text(
            '网络异常，请重试',
          ),
          const SizedBox(height: 10),
          ElevatedButton(
            child: const Padding(
              padding: EdgeInsets.only(
                left: 10,
                right: 10,
              ),
              child: Text('点击重试'),
            ),
            onPressed: () => print('点击重试'),
          ),
          const SizedBox(height: 100),
        ],
      ),
    );
  }
}
